package hongsong.eqixueweb.common;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateFromat {
	public String getdate(){
		Calendar ca = Calendar.getInstance();
		int year = ca.get(Calendar.YEAR);//获取年份
		int month=ca.get(Calendar.MONTH)+1;//获取月份 
		int day=ca.get(Calendar.DATE);//获取日
		int minute=ca.get(Calendar.MINUTE);//分 
		int hour=ca.get(Calendar.HOUR);//小时 
		int second=ca.get(Calendar.SECOND);//秒
		String smonth=null;
		String sday=null;
		String sminute=null;
		String shour=null;
		String ssecond=null;
		if(month <10){
			smonth ="0"+String.valueOf(month);
		}else{
			smonth =String.valueOf(month);
		}
		if(day <10){
			sday ="0"+String.valueOf(day);
		}else{
			sday =String.valueOf(day);
		}
		if(minute <10){
			sminute ="0"+String.valueOf(minute);
		}else{
			sminute =String.valueOf(minute);
		}
		if(hour <10){
			shour ="0"+String.valueOf(hour);
		}else{
			shour =String.valueOf(hour);
		}
		if(second <10){
			ssecond ="0"+String.valueOf(second);
		}else{
			ssecond =String.valueOf(second);
		}
		return String.valueOf(year)+smonth+sday+shour+sminute+ssecond;
	}
	
	/*
	 * 获取当前时间的差值，返回yyyy-MM-dd HH:mm
	 */
	public String getdateformat(String arg){
		Calendar now = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(arg.length()==1 || arg.length()==0){
			return sdf.format(now.getTime()).toString().substring(0, 16);
		}
		String cha = arg.substring(arg.length()-1,arg.length());
		System.out.println(cha);
		String ydmh =arg.substring(0,arg.length()-1);
		System.out.println(ydmh);
		if(cha.equals("y")){
			now.add(Calendar.YEAR, Integer.parseInt(ydmh));
		}
		if(cha.equals("M")){
			now.add(Calendar.MONTH, Integer.parseInt(ydmh));
		}
		if(cha.equals("d")){
			now.add(Calendar.DAY_OF_MONTH, Integer.parseInt(ydmh));
		}
		if(cha.equals("h")){
			now.add(Calendar.HOUR_OF_DAY, Integer.parseInt(ydmh));
		}
		if(cha.equals("m")){
			now.add(Calendar.MINUTE, ydmh.indexOf(ydmh));
		}

		return sdf.format(now.getTime()).toString().substring(0, 16);
	}
	/*
	 * 获取当前时间的差值，返回yyyy-MM-dd HH:mm:ss
	 */
	public String getdateformatss(String arg){
		Calendar now = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(arg.length()==1 || arg.length()==0){
			return sdf.format(now.getTime());
		}
		String cha = arg.substring(arg.length()-1,arg.length());
		System.out.println(cha);
		String ydmh =arg.substring(0,arg.length()-1);
		System.out.println(ydmh);
		if(cha.equals("y")){
			now.add(Calendar.YEAR, Integer.parseInt(ydmh));
		}
		if(cha.equals("M")){
			now.add(Calendar.MONTH, Integer.parseInt(ydmh));
		}
		if(cha.equals("d")){
			now.add(Calendar.DAY_OF_MONTH, Integer.parseInt(ydmh));
		}
		if(cha.equals("h")){
			now.add(Calendar.HOUR_OF_DAY, Integer.parseInt(ydmh));
		}
		if(cha.equals("m")){
			now.add(Calendar.MINUTE, ydmh.indexOf(ydmh));
		}

		return sdf.format(now.getTime()).toString();
	}
	
	public static void main(String args[]){
		System.out.println(new DateFromat().getdateformat(""));
		
		System.out.println(new DateFromat().getdateformat("-1y"));
		System.out.println(new DateFromat().getdateformat("-1M"));
		System.out.println(new DateFromat().getdateformat("-1d"));
		System.out.println(new DateFromat().getdateformat("-1h"));
		System.out.println(new DateFromat().getdateformat("-5m"));
	}
}
